package com.nstar.base.controls.view
{
	import com.nstar.base.controls.ButtonAssets;
	import com.nstar.base.util.Logger;
	import com.nstar.base.validate.ChineseNumberValidator;
	
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.system.System;
	
	import mx.containers.HBox;
	import mx.containers.TitleWindow;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.Application;
	import mx.effects.Effect;
	import mx.effects.Move;
	import mx.effects.Parallel;
	import mx.effects.Resize;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;

	/**
	 * 弹出窗口显示提示信息
	 * 
	 * 提供了几个功能：
	 * 	1.保存数据到本地，目前还未开放具体的保存位置
	 * 	2.打开本地保存的数据
	 * 	3.复制数据到剪贴板
	 * 	
	 * 	@author:zhucm
	 * 	@2007.8.11
	 */ 
	public class LoggerViewClass extends TitleWindow
	{
		public var btnClear:Button ;
		public var btnCopy:Button;
		public var btnClose:Button;
		public var logger:TextArea;
		public var btnSave:Button;
		public var btnOpen:Button;
		public var txtName:TextInput;
		public var buttonBar:HBox;
		public var btnHide:Button;
		
		[Bindable]
		private var _text:String = "";
		private var so:SharedObject;
		
		[Bindable]public var eShowEffect:Effect;
		[Bindable]public var eHideEffect:Effect;
		
		private var oldX:Number;
		private var oldY:Number;
		
		private var log:Logger = Logger.getInstance(this);
		
		public var fileName:ChineseNumberValidator = new ChineseNumberValidator();
		
		[Bindable]
		public function set text(value:String):void{
			this._text = value;
		}
		
		[Bindable]
		public function get text():String{
			return this._text;
		}
		
		public function LoggerViewClass()
		{
			//TODO: implement function
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		//初始化s
		function init(event:FlexEvent):void{
			btnClear.addEventListener(MouseEvent.CLICK,clearHandler);
			btnCopy.addEventListener(MouseEvent.CLICK,copyHandler);
			btnClose.addEventListener(MouseEvent.CLICK,closeHandler);
			btnSave.addEventListener(MouseEvent.CLICK,saveHandler);
			btnOpen.addEventListener(MouseEvent.CLICK,openHandler);
			btnHide.addEventListener(MouseEvent.CLICK,showButtons);
			this.addEventListener(CloseEvent.CLOSE,closeHandler);
			eShowEffect = showEffect();
			eHideEffect = hideEffect();
		}
		
		function showButtons(event:MouseEvent):void{
			if (buttonBar.width==0){
				event.target.setStyle("icon",ButtonAssets.rightIcon);
				event.target.toolTip = "隐藏面板其它按钮";
				buttonBar.width=430;
			}else{
				event.target.setStyle("icon",ButtonAssets.leftIcon);
				event.target.toolTip = "显示面板其它按钮";
				buttonBar.width=0;
			}
			
		}
		
		//加载保存的信息
		function openHandler(event:MouseEvent):void{
			if (txtName.text==""){
				Alert.show("请输入需要打开的名称!","提示信息");
				return;
			}
			so = SharedObject.getLocal("common/"+txtName.text);
			
			if (so.data.info!=null){
				this.text = so.data.info;
			}
		}
		
		//清除日志
		function clearHandler(event:MouseEvent):void{
			log.clear();
			logger.text = "";
		}
		//复制日志
		function copyHandler(event:MouseEvent):void{
			System.setClipboard( _text );
			
		}
		
		//关闭窗口
		function closeHandler(event:*):void{
			PopUpManager.removePopUp(this);
		}
		
		//保存日志
		function saveHandler(event:MouseEvent):void{
			if (txtName.text==""){
				Alert.show("请输入需要保存的名称!","提示信息");
				return;
			}
			
			so = SharedObject.getLocal("common/"+txtName.text);
			so.data.info = 	logger.text;
			so.flush();		
		}
		
		private function showEffect():Effect{
			var app:* = Application.application;
			var par:Parallel = new Parallel(this);
			var move:Move = new Move();
			move.xFrom = mouseX;
			move.yFrom = mouseY;
			oldX = mouseX;
			oldY = mouseY;
			
			move.xTo = (app.width - this.width)/2;
			move.yTo = (app.height - this.height)/2;
			
			var resize:Resize = new Resize();
			resize.widthFrom = 0;
			resize.heightFrom = 0;
			resize.widthTo = 800;
			resize.heightTo = 450;
			
			par.addChild(move);
			par.addChild(resize);
			return par;
		}
		
		private function hideEffect():Effect{
			var app:* = Application.application;
			var par:Parallel = new Parallel(this);
			var move:Move = new Move();
			move.xFrom = (app.width - this.width)/2;
			move.yFrom = (app.height - this.height)/2;
			move.xTo = oldX;
			move.yTo = oldY;
			
			var resize:Resize = new Resize();
			resize.widthFrom = 800;
			resize.heightFrom = 450;
			resize.widthTo = 0;
			resize.heightTo = 0;
			
			par.addChild(move);
			par.addChild(resize);
			return par;
		}		
		
	}
}